# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
System Role

The system role is responsible for managing an XMPP connection
and maintaining the application's state on the XMPP network.
"""

from kestrel.core.xmpp import Roster, Client, Component
from kestrel.core.system import Interface, Policy


class SystemRole(object):
    """
    The system role is responsible for managing an XMPP connection
    and maintaining the application's state on the XMPP network.

    Configuration entries for the modules in this role:
      system/XMPP
      system/interface

    Events raised by this module:
      system/start
      system/shutdown
    """
    def __init__(self):
        """
        Set the name of the role and define the default module
        implementations.
        """
        self.name = 'system'
        self.defaults = {'system/policy': Policy,
                         'system/interface': Interface,
                         'system/XMPP': Client,
                         'system/roster': Roster}
        self.modules = {}

    def register(self, kernel):
        """
        Instantiate and register the role's modules. The actual
        module implementations can be swapped by setting the
        appropriate configuration entries.
        """
        for module in self.defaults:
            impl = kernel.config(module, self.defaults[module])
            self.modules[module] = impl(kernel)

        kernel.event('system/start')

    def unregister(self, kernel):
        """Unregister the role's modules from the kernel."""
        kernel.event('system/shutdown')
